package cn.com.inossem.operation;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.os.Parcelable;
import cn.com.inossem.config.WSConfig;
import cn.com.inossem.db.DatabaseHelper;
import cn.com.inossem.model.BaGuaListInfoBean;
import cn.com.inossem.network.NetworkConnect;
import cn.com.inossem.requestmanager.WooRequestFactory;

import com.foxykeep.datadroid.exception.ConnectionException;
import com.foxykeep.datadroid.exception.CustomRequestException;
import com.foxykeep.datadroid.exception.DataException;
import com.foxykeep.datadroid.requestmanager.Request;
import com.foxykeep.datadroid.service.RequestService.Operation;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class BaGuaListOperation implements Operation {
	public static final String LIST_OPERATION_KEY = "list_operation_key";
	public static final String LIST_OPERATION_PAGENUM = "list_operation_pagenum";
	public static final String LIST_OPERATION_PARENTID = "list_operation_parentid";

	public static Bundle bundle;

	Context content = null;

	@Override
	public Bundle execute(Context arg0, Request request)
			throws ConnectionException, DataException, CustomRequestException {
		content = arg0;

		String key = request.getString(LIST_OPERATION_KEY);
		String pagenum = request.getString(LIST_OPERATION_PAGENUM);
		String parentid = request.getString(LIST_OPERATION_PARENTID);

		NetworkConnect connection = new NetworkConnect(WSConfig.LOCAL_URL
				+ "gossip/findGossip");
		connection.setPostText("{\"key\":\"" + key + "\",\"pageNum\":\""
				+ pagenum + "\",\"parentID\":\"" + parentid + "\"}");
		System.out.println("{\"key\":\"" + key + "\",\"pageNum\":\"" + pagenum
				+ "\",\"parentID\":\"" + parentid + "\"}");
		String body = connection.execute();

		System.out.println("BaGuaListOperation:" + body);
		bundle = new Bundle();
		try {
			JSONObject jsonObject = new JSONObject(body);
			int errorFlag = jsonObject.getInt("errorFlag");
			List<BaGuaListInfoBean> retList;
			if (errorFlag == WSConfig.RESULT_SUCESS) {
				
				int total = (Integer) jsonObject.get("total");
				String listGossip = jsonObject.get("listGossip").toString();
				System.out.println("listGossip:" + listGossip);
				Gson gson = new Gson();
				// BaGuaListInfoBean bagualist = datagson.fromJson(listGossip,
				// BaGuaListInfoBean.class);

				retList = gson.fromJson(listGossip,
						new TypeToken<List<BaGuaListInfoBean>>() {
						}.getType());

				// bundle.putParcelable(WooRequestFactory.RESPONSE_BAGUALIST_DATA,
				// bagualist);
				bundle.putParcelableArrayList(
						WooRequestFactory.RESPONSE_BAGUALIST_DATA,
						(ArrayList<? extends Parcelable>) retList);
				
				bundle.putInt("total", total);
			} else {
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}

		return bundle;
	}

	public void charu(String tabname, ContentValues contentValues,
			String whereClause) {
		DatabaseHelper dh = new DatabaseHelper(content);
		SQLiteDatabase db = dh.getWritableDatabase();

		db.update(tabname, contentValues, whereClause, null);

		db.close();
		dh.close();
	}
}
